/**
@file
	PollMessageDevice.h
@brief
	PollMessageDevice.
@author
	GengYong
@revision
	2005/11/27 * Created by GengYong.
*/

#ifndef POLLMESSAGEDEVICE_H
#define POLLMESSAGEDEVICE_H

#if	defined(WIN32)
#	error Microsoft Windows platform not supported epoll, only linux-2.6 kernel or newer supported.
#endif

#include "../../include/TMessageRouter.h"
#include <sys/epoll.h>

DECLARE_MESSAGE_ROUTER(PollMessageRouter);

class PollMessageRouter:public SAF::IMessageRouter
{
public: 
	virtual bool Init(SAF::IKernel*, SAF::IConfig*);
	virtual void Release();
public:
	virtual bool SendNetMessage(const SAF::IConnection *, const SAF::NetMessage&, const SAF::NetBuffer * = 0, unsigned int count_of_buffer = 0);
	virtual bool RecvNetMessage(SAF::IConnection *&, SAF::NetPackage&, unsigned int time);
	virtual bool DropConnection(const SAF::IConnection *);
public:
	typedef struct ProfileList
	{		
		int				sock;
		int				send_left;
		int				recv_left;
		int				bytes_sent;
		int				bytes_recv;		
		char *			send_ptr;
		char *			recv_ptr;
		char *			proc_ptr;
		char *			sendbuf;
		char *			recvbuf;
		void *			key;
		SAF::IConnection *	conn;
		ProfileList *	next;
	} ProfileList;

	typedef struct MessageDescriptor
	{
		PollMessageRouter::ProfileList *	profile;
		SAF::NetPackage						package;
	} MessageDescriptor;

public:
	int					m_pollHandle;	
	int					m_waitTimeOut;	
	int					m_connections;	
	int					m_msgDescCount;
	int					m_msgDescCursor;	
	int					m_maxConnCount;
	int					m_recvBufLength;
	int					m_sendBufLength;
	MessageDescriptor * m_msgDescStack;
	epoll_event *		m_eventBuffer;
	ProfileList *		m_rootProfile;
	ProfileList *		m_profileBase;
	ProfileList *		m_profileHead;	
	ProfileList *		m_profileTail;	
	SAF::IKernel *		m_hostKernel;
};

#endif	//#ifndef POLLMESSAGEDEVICE_H
